package com.six.apidemic.activity;

import static com.six.apidemic.httpapi.Constract.SUCC_CODE;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.lzy.okgo.callback.StringCallback;
import com.six.apidemic.R;
import com.six.apidemic.bean.LoginCallbackBean;
import com.six.apidemic.bean.RegisterCallbackBean;
import com.six.apidemic.databinding.ActivityRegisterBinding;
import com.six.apidemic.httpapi.Constract;
import com.six.apidemic.httpapi.HTTPAPI;
import com.six.apidemic.util.GsonUtils;
import com.six.apidemic.util.LogUtils;
import com.six.apidemic.util.SpUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {

   ActivityRegisterBinding binding;
   //0用户 1企业 2流调人员
   String type = "0";
   List<TextView> listTextViews = new ArrayList<>();

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      binding = DataBindingUtil.setContentView(this,R.layout.activity_register);
      binding.setActivity(this);
      initView();
      initData();
   }

   protected void initView() {
      binding.btModuleRegister.setOnClickListener(this);
      binding.tvModuleFirm.setOnClickListener(this);
      binding.tvModuleFlow.setOnClickListener(this);
      binding.tvModuleOrdinaryPeople.setOnClickListener(this);
      binding.tvModuleLogin.setOnClickListener(this);
   }

   protected void initData() {
      listTextViews.add(binding.tvModuleFirm);
      listTextViews.add(binding.tvModuleFlow);
      listTextViews.add(binding.tvModuleOrdinaryPeople);
   }


   @Override
   public void onClick(View view) {
      int id = view.getId();
      if (id == R.id.btModuleRegister){
         String name = binding.etModuleName.getText().toString().trim();
         if (TextUtils.isEmpty(name)) {
            showToast("请输入姓名");
            return;
         }
         String address = binding.etModuleAddress.getText().toString().trim();
         if (TextUtils.isEmpty(address)) {
            showToast("请输入地址");
            return;
         }
         String age = binding.etModuleAge.getText().toString().trim();
         if (TextUtils.isEmpty(age)) {
            showToast("请输入年龄");
            return;
         }
         String sex = binding.etModuleSex.getText().toString().trim();
         if (TextUtils.isEmpty(sex)) {
            showToast("请输入性别");
            return;
         }
         String card = binding.etModuleCard.getText().toString().trim();
         if (TextUtils.isEmpty(card)) {
            showToast("请输入身份证号码");
            return;
         }
         String phoneNumber = binding.etModulePhoneNumber.getText().toString().trim();
         if (TextUtils.isEmpty(phoneNumber)) {
            showToast("请输入手机号码");
            return;
         }
         String pwd = binding.etModulePwd.getText().toString().trim();
         if (TextUtils.isEmpty(pwd)) {
            showToast("请输入密码");
            return;
         }
         String pwdAgain = binding.etModulePwdAgain.getText().toString().trim();
         if (TextUtils.isEmpty(pwdAgain)) {
            showToast("请再次输入密码");
            return;
         }else if (!pwd.equals(pwdAgain)){
            showToast("两次输入不一致");
            binding.etModulePwdAgain.setText("");
            return;
         }

         /**
          * 注册
          * @param name      姓名
          * @param address   地址
          * @param age       年龄
          * @param idCard    身份证号码
          * @param mobile    电话号码
          * @param password  密码
          * @param sex       性别
          * @param status    状态 //0:正常 1:封控 2:密接 3:次密接 4:阳性
          * @param type      角色 0用户 1企业 2流调人员
          */
         toRegister(name,address,age,card,phoneNumber,pwd,sex,"0",type);
      }else if (id == R.id.tvModuleFirm){
         type = "1";
         checkTextView(binding.tvModuleFirm);
      }else if (id == R.id.tvModuleFlow){
         type = "2";
         checkTextView(binding.tvModuleFlow);
      }else if (id == R.id.tvModuleOrdinaryPeople){
         type = "0";
         checkTextView(binding.tvModuleOrdinaryPeople);
      }else if (id == R.id.tvModuleLogin){
         startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
         finish();
      }
   }

   //角色选择点击更改颜色
   public void checkTextView(TextView tvModule){
      for(TextView tv : listTextViews){
         tv.setTextColor(getResources().getColor(R.color.black));
      }
      tvModule.setTextColor(getResources().getColor(R.color.color_main));
   }

   /**
    * 注册
    * @param name      姓名
    * @param address   地址
    * @param age       年龄
    * @param idCard    身份证号码
    * @param mobile    电话号码
    * @param password  密码
    * @param sex       性别
    * @param status    状态 //0:正常 1:封控 2:密接 3:次密接 4:阳性
    * @param type      角色 0用户 1企业 2流调人员
    */
   public void toRegister(String name, String address, String age, String idCard, String mobile, String password,
                          String sex,String status, String type){
      HTTPAPI.getInstance().toRegister(name,address,age,idCard,mobile,password,sex,status,type,new StringCallback(){
         @Override
         public void onSuccess(String s, Call call, Response response) {
            LogUtils.e("注册成功-->"+ s);
            RegisterCallbackBean bean = GsonUtils.GsonToBean(s,RegisterCallbackBean.class);
            if (bean.getCode() == Constract.SUCC_CODE) {
               showToast(bean.getMessage());
               toLogin(name,password);
            }else{
               showToast(bean.getMessage());
            }
         }

         @Override
         public void onError(Call call, Response response, Exception e) {
            LogUtils.e("注册失败-->"+ e.getMessage());
         }
      });
   }

   /**
    * 登录
    * @param name
    * @param pwd
    */
   public void toLogin(String name,String pwd){
      HTTPAPI.getInstance().toLogin(name,pwd,new StringCallback(){
         @Override
         public void onSuccess(String s, Call call, Response response) {
            LogUtils.e("登录成功-->"+ s);
            LoginCallbackBean bean = GsonUtils.GsonToBean(s,LoginCallbackBean.class);
            if (bean.getCode() == SUCC_CODE) {
               SpUtils.putBean("module_user_bean",bean.getResult().getUser());
               SpUtils.put("module_token",bean.getResult().getToken());
               startActivity(new Intent(RegisterActivity.this,MainActivity.class));
               SpUtils.put("module_is_login","1");
               finish();
            }
         }

         @Override
         public void onError(Call call, Response response, Exception e) {
            LogUtils.e("登录失败-->"+ e.getMessage());
         }
      });
   }
}
